package ps.client.gui.userlist;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


@SuppressWarnings("serial")
public class UserListCellRenderer extends JComponent implements ListCellRenderer {

    public static Color COLOR_BG = new Color(80, 80, 80);
    
    private static Color COLOR_FG_USER_NAME = new Color(100, 255, 150);
    private static Color COLOR_FG_USER_NAME_ADMIN = new Color(50, 255, 50);
    
    private static Font FONT_USER_NAME = new Font("Arial", Font.BOLD, 12);
    private static Font FONT_PING_LOSS = new Font("Arial", Font.PLAIN, 10);
    
    JPanel contentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1));
    
    JLabel iconLabel = new JLabel();
    
    JLabel userNameLabel = new JLabel();
    
    JPanel pingLossPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
    JLabel pingLabel = new JLabel();
    JLabel lossLabel = new JLabel();
    
    ImageIcon greenIcon = new ImageIcon(getClass().getResource("green.png"));
    ImageIcon yellowIcon = new ImageIcon(getClass().getResource("yellow.png"));
    ImageIcon redIcon = new ImageIcon(getClass().getResource("red.png"));
    ImageIcon grayIcon = new ImageIcon(getClass().getResource("gray.png"));
    ImageIcon fuchsiaIcon = new ImageIcon(getClass().getResource("fuchsia.png"));
    ImageIcon blueIcon = new ImageIcon(getClass().getResource("blue.png"));
    
    public UserListCellRenderer() {
        setLayout(new BorderLayout());
        add(contentPanel, BorderLayout.CENTER);
        
        contentPanel.setPreferredSize(new Dimension(150, 16));
        contentPanel.setBackground(COLOR_BG);
        contentPanel.add(iconLabel);
        contentPanel.add(userNameLabel);
        contentPanel.add(pingLossPanel);
        
        iconLabel.setPreferredSize(new Dimension(16, 14));
        iconLabel.setIcon(greenIcon);
        
        userNameLabel.setHorizontalAlignment(JLabel.LEFT);
        userNameLabel.setVerticalAlignment(JLabel.BOTTOM);
        userNameLabel.setForeground(COLOR_FG_USER_NAME);
        userNameLabel.setFont(FONT_USER_NAME);
        userNameLabel.setPreferredSize(new Dimension(90, 14));
        
        pingLossPanel.setPreferredSize(new Dimension(40, 14));
        pingLossPanel.add(pingLabel);
        pingLossPanel.add(lossLabel);
        
        pingLabel.setHorizontalAlignment(JLabel.RIGHT);
        pingLabel.setVerticalAlignment(JLabel.BOTTOM);
        pingLabel.setForeground(COLOR_FG_USER_NAME);
        pingLabel.setFont(FONT_PING_LOSS);
        
        lossLabel.setHorizontalAlignment(JLabel.RIGHT);
        lossLabel.setVerticalAlignment(JLabel.BOTTOM);
        lossLabel.setForeground(COLOR_FG_USER_NAME);
        lossLabel.setFont(FONT_PING_LOSS);
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        
        if (value != null) {
            
            UserListEntry entry = (UserListEntry)value;
            
            if (isSelected) {
                contentPanel.setBackground(Color.GRAY);
                userNameLabel.setBackground(Color.GRAY);
                pingLossPanel.setBackground(Color.GRAY);
                userNameLabel.setForeground(entry.admin ? COLOR_FG_USER_NAME_ADMIN : COLOR_FG_USER_NAME);
            } else {
                contentPanel.setBackground(COLOR_BG);
                userNameLabel.setBackground(COLOR_BG);
                userNameLabel.setForeground(entry.admin ? COLOR_FG_USER_NAME_ADMIN : COLOR_FG_USER_NAME);
    
                pingLossPanel.setBackground(COLOR_BG);
                pingLabel.setForeground(Color.GREEN);
                lossLabel.setForeground(Color.RED);
            }
            
            if (entry.groupNumber == 0) {
                userNameLabel.setText(entry.userName);
            } else {
                userNameLabel.setText(entry.userName + " | " + entry.groupNumber);
            }
            if (entry.ping > -1) {
                pingLabel.setText("" + entry.ping);
            }
            
            if (entry.linkDead) {
                iconLabel.setIcon(redIcon);
            } else if (entry.afk) {
                iconLabel.setIcon(fuchsiaIcon);
            } else if (!entry.logReadAvtive){
                iconLabel.setIcon(yellowIcon);
            } else {
                iconLabel.setIcon(greenIcon);
            }
        } else {
            return new JLabel();
        }
        
        return this;
    }

}
